<?php
class PHPUploadFile
{
	//$strFileInputTypeName 从页面来源中定义input的type=file的name属性值
	//$arrayFileTypes	被允许上传的文件类型列表
	//$extName	被允许上传的文件扩展名 ,值 为：$extName = 'html,txt,htm'; 
	//$intFileSize	被 允许上传的文件大小
	//$stringUploadDir	上传成功后文件保存的目录,请保证此目录 已经存在**
	public function uploadFile($strFileInputTypeName,$arrayFileTypes,$extName,$intFileSize,$uploadDir=0)
	{
                switch($uploadDir)
                {
                    case 0:
                    $stringUploadDir=$this->getDefaultUploadPathString();
                        break;
                    case 1:
                         $stringUploadDir=$this->getImageUploadPathString();
                        break;
                    case 2:
                        $stringUploadDir=$this->getFlashUploadPathString();
                        break;
                    default:
                        break;
                }
               
		if($this->checkFileType($strFileInputTypeName,$arrayFileTypes))
		{
			if($this->checkFileSize($strFileInputTypeName,$intFileSize))
			{
				if( $this->checkFileExt($_FILES [$strFileInputTypeName] ["name"],$extName))
				{
					//IE一切正常
					$oldFileName=$_FILES [$strFileInputTypeName] ["name"];
					$tmpFileName=$_FILES [$strFileInputTypeName] ["tmp_name"];
					$charid = strtoupper(md5(uniqid(mt_rand(), true))); //上传成功后保存的文件名 guid
					$path_parts = pathinfo ( $oldFileName );
			        $ext = $path_parts ["extension"];	//扩展名
			        $newFileName=gettimeofday(true).".".mt_rand().".".$ext;
			        $newFileFullName=$stringUploadDir."/" .$newFileName;
			        
			        move_uploaded_file ( $tmpFileName, $newFileFullName );
			        return $newFileFullName;
				}
				else 
				{
					echo "不允許上傳此類型文檔";
				}
				
			}
			else 
			{
				echo "超出規定的上傳文檔大小";
			}
		}
		else 
		{
			echo "不允許上傳此類型文檔";
		}
	}
	//检查文件类型，合格返回true,否则返回false
	private function checkFileType($strFileInputTypeName,$arrayFileTypes)
	{
		$result=false;
		foreach($arrayFileTypes as $fileType)
		{
			if($_FILES [$strFileInputTypeName] ["type"] ==$fileType )
			{
				$result=true;
				return  $result;
			}
		}
		
		return $result;
	}
	
	//检查文件大小，合格返回true,否则返回false
	private function checkFileSize($strFileInputTypeName,$intFileSize)
	{
		$result=false;
		if($_FILES [$strFileInputTypeName] ["size"] <= $intFileSize)
			$result=true;
		return $result;
	}
	private function checkFileExt($fname,$extname){ 
		//var  $extname = 'html,txt,htm'; 
		$path_parts = pathinfo ($fname );
		$ext = $path_parts ["extension"];
		$re = "/$ext/i";
		//echo $re."  ". $extname;
	
		if (preg_match ( $re, $extname)) //扩展名为:.php,.xml,.css,.js.......由数组$extname定义 
		{
			return true;
		}
		return false;
	}// end func 
	private function getDefaultUploadPathString()
	{
             $cs_parameter=new cs_parameter();
             $uploadPath=$cs_parameter->getUploadPath();
	     return $_SERVER["DOCUMENT_ROOT"].$uploadPath;//"/upload";//要从配置表里面取
	}
	private function getImageUploadPathString()
	{
             $cs_parameter=new cs_parameter();
             $uploadPath=$cs_parameter->getimgUploadPath();
	     return $_SERVER["DOCUMENT_ROOT"].$uploadPath;//"/upload";//要从配置表里面取
	}
        private function getFlashUploadPathString()
	{
             $cs_parameter=new cs_parameter();
             $uploadPath=$cs_parameter->getFlashUploadPath();
	     return $_SERVER["DOCUMENT_ROOT"].$uploadPath;//"/upload";//要从配置表里面取
	}
}
?>